﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace MoneyManagerNET
{
    using Presentation.ViewModel;
    using Presentation.View;
using System.Diagnostics;
using MoneyManagerNET.Presentation.Infrastructure.Binding;
    using System.Collections;
    using Mvvm.Infrastructure;
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            this.Resources["wrapper"]=s_wrapper;
        }

        static TraceSource trace = new TraceSource("Application");

        static Hashtable s_identityMap=new Hashtable();
        static WrapConverter s_wrapper;

        static App() {
            AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException); ;
            AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
            AppDomain.CurrentDomain.AssemblyResolve += (s, args) =>
            {
                var location = args.RequestingAssembly == null ? string.Empty : args.RequestingAssembly.Location;
                trace.TraceEvent(TraceEventType.Information, 0, "assembly resolve : {0}     from: {1}", args.Name, location);
                return null;
            };

            WrapConverter.s_is_design = false;
            s_wrapper = new WrapConverter(s_identityMap);
            

            trace.TraceEvent(TraceEventType.Start, 0, "View2ViewModel");
            Trace.Indent();

            //ViewLocator.View2ViewModel<CategyListViewModel,CategoriesView>();
            //ViewLocator.View2ViewModel<ContragentListViewModel, ContragentView>();
            //ViewLocator.View2ViewModel<AccountListViewModel, AccountListView>();
            //ViewLocator.View2ViewModel<TransactionViewModel, TransactionView>();
            //ViewLocator.View2ViewModel<CreateAccountViewModel, CreateAccountView>();
            //ViewLocator.View2ViewModel<EditAccountViewModel, EditAccountView>();
            //ViewLocator.View2ViewModel<StockInvestmentViewModel, StockInvestmentView>();
            //ViewLocator.View2ViewModel<RelocateCategoryViewModel, RelocateCategoryView>();
            //ViewLocator.View2ViewModel<RelocateContragentViewModel, RelocateContragentView>();
            //ViewLocator.View2ViewModel<AssetViewModel, AssetView>();
            //ViewLocator.View2ViewModel<StockAccountListViewModel, StockAccountListView>();


            //ViewLocator.View2ViewModel<SummaryOfAccountsReportViewModel, SummaryOfAccountsReportView>();
            //ViewLocator.View2ViewModel<SummaryOfStocksReportViewModel, SummaryOfStocksReportView>();


            Trace.Unindent();
            trace.TraceEvent(TraceEventType.Stop, 0, "View2ViewModel");
        }

        static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
        {
            AppDomain.CurrentDomain.FirstChanceException -= CurrentDomain_FirstChanceException;
            try
            {
                trace.TraceEvent(TraceEventType.Information, 0, "assembly loaded : {0}", args.LoadedAssembly.Location);
            }
            catch { }
            AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

        }

        static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
        {
            AppDomain.CurrentDomain.FirstChanceException -= CurrentDomain_FirstChanceException;
            { try { trace.TraceData(TraceEventType.Critical, 0, e.Exception); } catch { } }
            AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

        }





        [PreEmptive.Attributes.Setup(CustomEndpoint = "so-s.info/PreEmptive.Web.Services.Messaging/MessagingServiceV2.asmx")]
        protected override void OnStartup(StartupEventArgs e)
        {

            trace.TraceEvent(TraceEventType.Start, 1, "OnStartup");
            Trace.Indent();

            base.OnStartup(e);
        }

        [PreEmptive.Attributes.Teardown()]
        protected override void OnExit(ExitEventArgs e)
        {

            base.OnExit(e);
            Trace.Unindent();
            trace.TraceEvent(TraceEventType.Stop, 1, "OnExit");

        }

        
    }
}
